import type {
  CodeType,
  PatientList,
  User,
  UserInfo,
  Patient
} from '@/types/user'
import { request } from '@/utils/request'

/**
 * 用密码登录
 * @param mobile 手机号
 * @param password 密码
 */
export const loginByPassword = (mobile: string, password: string) =>
  request<User>('/login/password', 'POST', {
    mobile,
    password
  })

/**
 * 获取手机验证码
 * @param mobile 手机号
 * @param type 验证码类型
 */
export const sendMobileCode = (mobile: string, type: CodeType) => {
  return request('/code', 'GET', {
    mobile,
    type
  })
}

/**
 * 手机验证码登录
 * @param mobile 手机号
 * @param code 验证码
 */
export const loginByMobile = (mobile: string, code: string) =>
  request('/login', 'POST', {
    mobile,
    code
  })

/**
 * 我的获取个人用户信息
 */
export const getUserInfo = () => {
  return request<UserInfo>('/patient/myUser')
}

/**
 * 获取患者列表
 */
export const getPatientList = () => request<PatientList>('/patient/mylist')

/**
 * 添加患者
 * @param patient 患者信息 表单四个字段
 */
export const addPatient = (patient: Patient) => {
  return request('/patient/add', 'POST', patient)
}

/**
 * 编辑患者
 * @param patient 患者信息 多一个id字段
 */
export const updatePatient = (patient: Patient) =>
  request('/patient/update', 'PUT', patient)

/**
 * 删除患者
 * @param id 患者id
 */
export const delPatient = (id: string) => {
  return request(`/patient/del/${id}`, 'DELETE')
}

// 查询患者详情
export const getPatientDetail = (id: string) =>
  request<Patient>(`/patient/info/${id}`)

export const loginByQQ = (openId: string) =>
  request<User>('/login/thirdparty', 'POST', { openId, source: 'qq' })

export const bindMobile = (data: {
  mobile: string
  code: string
  openId: string
}) => request<User>('/login/binding', 'POST', data)
